package com.momin.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yyj
 * @Package com.momin.common.valid
 * @date 2021/4/28 上午9:09
 */
public class ListValuesConstraintValidator implements ConstraintValidator<ListValues,Integer> {

    private Set set = new HashSet();

    /**
     * 初始化方法
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValues constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if(vals.length>0){
            for(int val:vals){
                set.add(val);
            }
        }
    }

    /**
     * 判断是否成功
     * @param integer
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        if (set.contains(integer)) {
            return true;
        }
        return false;
    }
}
